//
//  CAGradientLayer+Develop.swift
//  Swaddle
//
//  Created by memory on 2021/5/17.
//

import Foundation

import UIKit

public enum LYGradientLayerDirectionOption {
    case top2down
    case left2right
    case topLeft2downRight
    case topRight2downLeft
    case other
}

public extension YLDevelop where Base: CAGradientLayer {
    
    /// 快速创建一个渐变的layer
    ///
    /// - Parameter colors: 渐变颜色
    /// - Parameter frame: 渐变范围
    /// - Parameter direction: 渐变方向
    /// - Returns: gradientLayer
    static func gradientLayerWith(colors: [UIColor], frame: CGRect, direction: LYGradientLayerDirectionOption) -> CAGradientLayer {
        
        let layer = CAGradientLayer()
        layer.frame = frame
        layer.colors = colors.map { return $0.cgColor }
        var start = CGPoint(x: 0.5, y: 0)
        var end = CGPoint(x: 0.5, y: 1)
        switch direction {
        case .top2down:
            start = CGPoint(x: 0.5, y: 0)
            end = CGPoint(x: 0.5, y: 1)
        case .left2right:
            start = CGPoint(x: 0, y: 0.5)
            end = CGPoint(x: 1, y: 0.5)
        case .topLeft2downRight:
            start = CGPoint(x: 0, y: 0)
            end = CGPoint(x: 1, y: 1)
        case .topRight2downLeft:
            start = CGPoint(x: 1, y: 0)
            end = CGPoint(x: 0, y: 1)
        case .other:
            break
        }
        layer.startPoint = start
        layer.endPoint = end
        return layer
    }
}

public extension YLDevelop where Base:UIView{
    func addGardiendLayerWith(colors: [UIColor], frame: CGRect, direction: LYGradientLayerDirectionOption){
        let gradientLayer = CAGradientLayer.yl.gradientLayerWith(colors: colors, frame: frame, direction: direction)
        base.layer.insertSublayer(gradientLayer, at: 0)
    }
}
